package com.example.demo.core;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;

import static com.example.demo.constants.ErrorStatusConstants.*;

@Data
@JsonPropertyOrder(value = "code,msg,data")
public class ApiResponse<T> {

    private String code;
    private String msg;
    private T data;

    public ApiResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResponse<T> ok() {
        return new ApiResponse<>(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MSG, null);
    }

    public static <T> ApiResponse<T> ok(String msg, T data) {
        return new ApiResponse<>(DEFAULT_SUCCESS_CODE, msg, data);
    }

    public static <T> ApiResponse<T> ok(T data) {
        return new ApiResponse<>(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MSG, data);
    }

    public static <T> ApiResponse<T> fail() {
        return new ApiResponse<>(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MSG, null);
    }

    public static <T> ApiResponse<T> fail(String msg) {
        return new ApiResponse<>(DEFAULT_ERROR_CODE, msg, null);
    }

    public static <T> ApiResponse<T> fail(String msg, T data) {
        return new ApiResponse<>(DEFAULT_ERROR_CODE, msg, data);
    }

    public static <T> ApiResponse<T> fail(String code, String msg, T data) {
        return new ApiResponse<>(code, msg, data);
    }

    public static <T> ApiResponse<T> fail(String code, String msg) {
        return new ApiResponse<>(code, msg, null);
    }
}
